package com.wish.wishfinancial_service.service.impl;

import com.alibaba.fastjson.JSON;
import com.wish.wishfinancial_service.entity.TCwPaymentPlan;
import com.wish.wishfinancial_service.entity.TJrProduct;
import com.wish.wishfinancial_service.entity.TSfContract;
import com.wish.wishfinancial_service.enums.ContractTypeEnum;
import com.wish.wishfinancial_service.enums.PayPlanZdType;
import com.wish.wishfinancial_service.enums.PayStatusEnum;
import com.wish.wishfinancial_service.enums.PaymentTypeEnum;
import com.wish.wishfinancial_service.mapper.TCwPaymentPlanMapper;
import com.wish.wishfinancial_service.mapper.TJrProductMapper;
import com.wish.wishfinancial_service.mapper.TSfContractMapper;
import com.wish.wishfinancial_service.service.ITSfContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dto.financialResponse.SfContractDetailsResponseDTO;
import dto.houseForeclosureDto.RequestSearchSfsDto;
import dto.response.ResponseSearchSfsDto;
import enums.BusinessCostTypesEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.MyJinRongUtils;
import vo.ResultEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wish.wishfinancial_service.entity.TSfContract;
import com.wish.wishfinancial_service.mapper.TSfContractMapper;
import com.wish.wishfinancial_service.service.ITSfContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weijiaz
 * @since 2021-04-10
 */
@Service
@Slf4j
public class TSfContractServiceImpl extends ServiceImpl<TSfContractMapper, TSfContract> implements ITSfContractService {
    @Autowired
    private TJrProductMapper tJrProductMapper;
    @Autowired
    private TCwPaymentPlanMapper tCwPaymentPlanMapper;

    @Override
    public SfContractDetailsResponseDTO getSfContractStagingCalculation(Long sfContractId, Long productId) {
        log.info("getSfContractStagingCalculation查询收房贷计算详情处理Start...");
        log.info("----查询收房合同详情查询处理详情入参：{}", JSON.toJSONString(sfContractId) + JSON.toJSONString(productId));
        TSfContract sfce = baseMapper.selectById(sfContractId);
        SfContractDetailsResponseDTO sfdto = new SfContractDetailsResponseDTO();
        BeanUtils.copyProperties(sfce, sfdto);
        // 查询收房分期
        List<TCwPaymentPlan> payList = getCwPaymentPlanList(sfContractId);
        TJrProduct product = tJrProductMapper.selectById(productId);
        // 计算授信额
        Double creditLine = MyJinRongUtils.div(product.getCreditLine(), 100, 2);
        double balance = 0;
        if (payList.size() > 0) {
            for (TCwPaymentPlan pay : payList) {
                balance = MyJinRongUtils.add(balance, pay.getPaymentAmount().doubleValue());
            }
        }
        // 获取期数
        int size = payList.size();
        Integer paymentType = sfce.getPaymentType();
        double period = MyJinRongUtils.mul(size, paymentType);
        // 期数
        sfdto.setPeriods(new Double(period).intValue());
        // 总金额
        sfdto.setBalance(balance);
        // 授信总额
        double sxBalance = MyJinRongUtils.mul(sfdto.getBalance(), creditLine);
        sfdto.setSxBalance(sxBalance);
        log.info("getSfContractStagingCalculation查询收房贷计算详情处理End...");
        return sfdto;
    }

    @Override
    public ResultEntity searchSfContractListXy(RequestSearchSfsDto requestSearchSfsDto) {


        return null;
    }

    @Override
    public List<ResponseSearchSfsDto> searchSfAllNotDissolution(RequestSearchSfsDto requestSearchSfsDto) {
        log.info("searchSfAllNotDissolution查询收房合同详情(加判断,是否为未解约)查询处理Start...");
        log.info("----查询收房合同详情(加判断,是否为未解约)入参：{}", JSON.toJSONString(requestSearchSfsDto));
        List<ResponseSearchSfsDto> recordList = baseMapper.searchSfAllNotDissolution(requestSearchSfsDto);
        // 设置分页结果
        log.info("----输出结果：{}", JSON.toJSONString(recordList));
        log.info("searchSfAllNotDissolution查询收房合同详情(加判断,是否为未解约)处理End.");

        return recordList;

    }

    // 查询收房分期 - 付款计划（ 未付款）
    private List<TCwPaymentPlan> getCwPaymentPlanList(Long sfContractId) {
        Map<String, Object> param = new HashMap<>();
        param.put("contract_type", ContractTypeEnum.SF.getIndex());
        param.put("contract_id", sfContractId);
        param.put("pay_type", PaymentTypeEnum.SFFK.getIndex());
        param.put("pay_state", PayStatusEnum.WFK.getIndex());
        param.put("bill_type", PayPlanZdType.PTZD.getIndex());
        param.put("item_type", BusinessCostTypesEnum.ZUJIN.getIndex());

        return tCwPaymentPlanMapper.selectByMap(param);
    }


}
